programming4us
           
 
 
Windows Phone

Developing Applications for Windows Phone 7 : Data Binding (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/27/2010 11:41:08 AM

Element Binding

Data binding also allows you to create bindings that allow you to create a linkage between XAML elements. This is called Element Binding. To use element binding, you can specify the ElementName as part of the binding syntax like so:

<Slider Minimum="10"
Maximum="36"
x:Name="fontSizeSlider" />
<TextBox FontSize="{Binding Value, ElementName=fontSizeSlider}"
Text="Make It Grow" />

The size of the font in the TextBox is being set based on the value of the Slider (named fontSizeSlider). In this way you can use elements in the XAML to supply data to other elements in the XAML. A more conventional use of element binding is to set the data context of a container based on the selected value of a control like so:

<ListBox ItemsSource="{Binding Games}"
x:Name="theList" />
<StackPanel DataContext="{Binding SelectedItem, ElementName=theList}">
<TextBlock>Name</TextBlock>
<TextBox Text="{Binding Name}" />
<TextBlock>Phone Number</TextBlock>
<TextBox Text="{Binding Phone}" />
</StackPanel>

In this example, the StackPanel is setting its DataContext to whatever item is selected in the ListBox. In this way you can show and/or edit the data in the StackPanelListBox. based on the selection of the

Converters

Data binding takes properties from objects and moves them into properties on controls. At times the types of the properties will not match or need some level of manipulation to work. That is where converters come in. Converters are stateless classes that can perform specific conversions during the binding process. In order to be a converter, a class must implement the IValueConverter interface. This interface has two methods: Convert and ConvertBack to allow for conversions in both directions during binding. For example, a simple converter to make dates show up as short date strings looks like so:

public class DateConverter : IValueConverter
{

public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
if (targetType == typeof(string) &&
value.GetType() == typeof(DateTime))
{
return ((DateTime)value).ToShortDateString();
}

// No Conversion
return value;

}

public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
if (targetType == typeof(DateTime) &&
value.GetType() == typeof(string))
{
DateTime newDate;

if (DateTime.TryParse((string)value, out newDate))
{
return newDate;
}
}

// No Conversion
return value;
}
}


Converters are created as resources (usually at the application level) like so:

<Application x:Class="PhoneControls.App"
xmlns="..."
xmlns:x="..."
xmlns:phone="..."
xmlns:shell="..."
xmlns:my="clr-namespace:PhoneControls">
<Application.Resources>
<my:DateConverter x:Key="dateConverter" />
</Application.Resources>
...
</Application>

By creating the converter at the application level, it can be used throughout the entire application. Finally we can now use the converter directly in our data binding like so:

<StackPanel DataContext="{Binding SelectedItem, ElementName=theList}">
<TextBlock>Name</TextBlock>
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<TextBlock>Phone Number</TextBlock>
<TextBox Text="{Binding PhoneNumber, Mode=TwoWay}" />
<TextBlock>Phone Number</TextBlock>
<TextBox Text="{Binding ReleaseDate,
Mode=TwoWay,
Converter={StaticResource dateConverter}}" />
</StackPanel>

During the conversion of the underlying data (in this case a DateTime) the DateConverter class is used. When moving the data from the source to the control, the Convert is called; when the data is pushed back to the source, the ConvertBack method is called. Like this example, converters are often used just for formatting and not real conversion[2].

[2] In Silverlight 4 the StringFormat markup extension option was added to simplify formatting during databinding but is not available in the Windows Phone 7 version of Silverlight (since its based on Silverlight 3). It should be available in future versions of Silverlight for the Windows Phone 7.

Data Binding Errors

By design, data binding errors do not cause exceptions. This behavior is desirable because the source of a data binding may enter a valid and invalid state a lot during the life of your application. Let’s take the example we saw earlier where we have a list of controls bound to the SelectedItem of a ListBox. When there is no selection in the ListBox the data binding is failing. Throwing an exception is that case would be the wrong thing to do. So as a developer you will need to need a way to actually see data binding failures. Luckily you can see them pretty clearly in the Visual Studio Output Window. When running your application, you can use the View menu to show the Output Window as shown in Figure 3.

Figure 3. View Output Window (ViewOutputWindow.bmp)


When data binding fails, it adds a debug message to the output window. For example, if you used the wrong path in a Binding (e.g. Title instead of Name) you could see this in the Output Window as seen in Figure 4.

Figure 4. Binding Error Shown in the Output Window (BindingErrorInOutputWindow.bmp)


It’s not only binding errors like bad paths that show up in the Output Window, but also bad conversions (really any exception). For example, if a user attempted to enter a bad date (e.g. 2/31/2010) into a date field, the output window shows that error too during data binding (as seen in ).

Figure 5. Conversion Error in the Output Window (ConversionErrorOutputWindow.bmp)



Other -----------------
- Developing Applications for Windows Phone 7 : Transformations and Animations
- Developing Applications for Windows Phone 7 : Controls
- Developing Applications for Windows Phone 7 : Visual Grammar
- Developing Applications for Windows Phone 7 : Visual Containers
- Developing Applications for Windows Phone 7 : What is XAML?
- Windows Phone 7 : Connecting a Bluetooth Headset
- Windows Phone 7 : Turning On Airplane Mode
- Windows Phone 7 : Updating Your Phone Software
- Windows Phone 7 : Finding a Lost Phone
- Windows Phone 7 : Locking Your Phone
- Windows Phone 7 : Importing Contacts from a SIM Card
- Windows Phone 7 : Silencing Your Phone
- Windows Phone 7 with Silverlight : Working with the Phone
- Writing Your First Phone Application - Adding Code (part 2)
- Writing Your First Phone Application - Adding Code (part 1)
- Developing Applications for Windows Phone 7 : Designing with Blend
- Developing Applications for Windows Phone 7 : Creating a New Project
- Developing Applications for Windows Phone 7 with Silverlight : Preparing Your Machine
- Windows Phone 7 : Picking Ringtones and Alerts
- Windows Phone 7 : Changing Themes and Wallpaper
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us